﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Collections;

namespace LiveNet.Contacts
{
    public class PhoneCollection
    {
        #region Private Data
        private ArrayList phones = new ArrayList();
        #endregion

        #region Constructors
        internal PhoneCollection()
        {
        }

        internal PhoneCollection(XmlNode xmlnode)
        {
            FromXml(xmlnode);
        }
        #endregion

        #region Internal Methods
        internal void FromXml(XmlNode xmlnode)
        {
            if (xmlnode == null) return;

            XmlNodeList nodes = xmlnode.SelectNodes("Phone");
            if (nodes != null)
            {
                foreach (XmlNode node in nodes)
                {
                    Add(new Phone(node));
                }
            }
        }

        internal void Add(Phone phone)
        {
            phones.Add(phone);
        }

        internal void Remove(Phone phone)
        {
            phones.Remove(phone);
        }

        internal void RemoveAll()
        {
            phones.Clear();
        }
        #endregion

        #region Public Properties
        public int Count
        {
            get { return phones.Count; }
        }

        public object[] ToArray
        {
            get { return phones.ToArray(); }
        }
        #endregion

        #region Public Methods
        public XmlDocument ToXml(OperationType op)
        {
            XmlDocument xmldoc = new XmlDocument();
            string xml = "<Phones>";
            foreach (Phone phone in this)
            {
                xml = xml + phone.ToXml(op).InnerXml;
            }
            xml = xml + "</Phones>";
            xmldoc.LoadXml(xml);

            return xmldoc;
        }

        public IEnumerator GetEnumerator()
        {
            return phones.GetEnumerator();
        }

        public Phone Find(int phoneID)
        {
            foreach (Phone phone in phones)
            {
                if (phone.ID == phoneID) return phone;
            }
            return null;
        }

        public Phone New()
        {
            return new Phone();
        }
        #endregion
    }
}
